/**
  ******************************************************************************
  * @file    Libraries/Device/TS32Fx/TS32Fx_LL_Driver/inc/ts32fx_ll_wdt.h
  * @author  TOPSYS Application Team
  * @version V1.0.0
  * @date    02-11-2018
  * @brief   This file contains all the WDT LL firmware functions.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2018 TOPSYS</center></h2>
  *
  *
  *
  ******************************************************************************
  */ 
  
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __TS32FX_LL_WDT_H
#define __TS32FX_LL_WDT_H

#ifdef __cplusplus
 extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "ts32fx.h"
     
/** @addtogroup TS32Fx_StdPeriph_Driver TS32Fx Driver
  * @{
  */
     
/** @addtogroup wdt_interface_gr WDT Driver
  * @ingroup  TS32Fx_StdPeriph_Driver
  * @{
  */ 

/** @addtogroup WDT_LL_Driver WDT LL Driver
  * @ingroup  wdt_interface_gr
  * @brief Mainly the driver part of the WDT module, which includes \b WDT \b Register 
  * \b Constants, \b WDT \b Exported \b Constants, \b WDT \b Exported \b Struct, \b WDT
  * \b Data \b transfers \b functions, \b WDT \b Initialization \b and \b WDT \b Configuration 
  * \b And \b Interrupt \b Handle \b function.
  * @{
  */

/* Exported types ------------------------------------------------------------*/

/* Exported constants --------------------------------------------------------*/

/** @defgroup WDT_LL_Register_Constants WDT LL Register Constants
  * @ingroup  WDT_LL_Driver
  * @brief    WDT LL register constant table definition
  *
  *
@verbatim   
  ===============================================================================
                                Register Constants
  ===============================================================================  
  
    Register Constants mainly encapsulates each bit in each group in the WDT 
    register. In the process of configuration, the macro definition can be directly 
    called to configure the WDT register, mainly for convenience. Understand the 
    configuration of the WDT.
    
@endverbatim
  *
  * @{
  */

/***** WDT_CON(WDT Config Register) *****/
/*! WDT_PSR Register TYPE_ENUM_LL_WDT_PSR : Wdt division factor rw registor  
 * wdt reset time = 1/32K*256*psr, must write key value 0x5555 before set wdt psr value.It is 8 after reset.  
 * division factor(psr)     about reset time  
 * 4'h0:no     div            8ms  
 * 4'h1:2      div            16ms  
 * 4'h2:4      div            32ms  
 * 4'h3:8      div            64ms  
 * 4'h4:16     div            128ms  
 * 4'h5:32     div            256ms  
 * 4'h6:64     div            512ms  
 * 4'h7:128    div            1024ms  
 * 4'h8:256    div            2048ms  
 * 4'h9:512    div            4096ms  
 * 4'ha:1024   div            8192ms  
 * 4'hb:2048   div            16384ms  
 * 4'hc:4096   div            32768ms  
 * 4'hd:8192   div            65536ms  
 * 4'he:16384  div            131072ms  
 * 4'hf:32768  div            262144ms  
 */
#define LL_WDT_CON_PSR(n)                       (((n)&0xF) << 0)
/*! Used to indicate that wdt enable or disable.  
 * 0 : disable, 1 : enable  
 * rw registor : Write key value 0xCCCC start wdt,0xDDDD close wdt.  
 */
#define LL_WDT_CON_WDTE                         (1UL << 4)
/*! Used to indicate that wdt int enable or disable.  
 * 0 : disable, 1 : enable  
 * ro registor  
 */
#define LL_WDT_CON_IE                           (1UL << 5)
/*! Used to indicate that wdt int count full or not.  
 * 0 : wdt count not full , 1 : wdt count full  
 * ro registor : Write key value 0xAAAA to clear the pending.  
 */
#define LL_WDT_CON_PENDING                      (1UL << 6)

/***** WDT_KEY Register *****/
/*! This key value is used to clear wdt pending.  
 *  time out reset mode: This key value must be written at regular intervals during, otherwise a restart will be happened. will also clears the WDT  
 *  time out interrupt mode: This key value be written can clear the wdt pending,when pending is 1.  
 */
#define LL_WDT_KEY_FEED_DOG                     (0xAAAA)
/*! This key value is used to visit wdtpsr registor.  
 *  Must write this key value before set wdt psr value.  
 */
#define LL_WDT_KEY_CAN_VISIT_PSR                (0x5555)
/*! This key value is used to start wdt.
 */
#define LL_WDT_KEY_START_DOG                    (0xCCCC)
/*! This key value is used to close wdt.
 */
#define LL_WDT_KEY_CLOSE_DOG                    (0xDDDD)

/*! This key value is used to enable wdt timeout interrupt.
 */
#define LL_WDT_KEY_EN_INT                       (0x55AA)
/*! This key value is used to disable wdt timeout interrupt, it is at wdt reset mode.  
 */
#define LL_WDT_KEY_DIS_INT                      (0xAA55)
/*! This key value is used to enable wdt wakeup.
 */
#define LL_WDT_KEY_EN_WAKEUP                    (0x5A5A)
/*! This key value is used to disable wdt wakeup.
 */
#define LL_WDT_KEY_DIS_WAKEUP                   (0xA5A5)
/*! Used to indicate that set key value.
 */
#define LL_WDT_KEY_SET(p_wdt, key)              (((p_wdt)->WDTKEY) = key)



/**
  * @}
  */

/** @defgroup WDT_LL_Exported_Constants WDT LL Exported Constants
  * @ingroup  WDT_LL_Driver
  * @brief    WDT LL external constant definition
  *
@verbatim   
  ===============================================================================
                                Exported Constants
  ===============================================================================  
  
    Exported Constants mainly restricts the partial configuration of the abstraction 
    layer by using the form of enumeration to facilitate the use and understanding of 
    the module configuration. For the specific enumeration meaning, please refer to 
    the annotation of each module.

@endverbatim
  *
  * @{
  */
  
/***** DRIVER API *****/



/***** LL API *****/

/**
  * @brief Enumeration constant for low layer wdt division factor
  */
typedef enum {
    /*! watchdog  division factor : 0
     */
    LL_WDT_PSR_0DIV = 0,
    /*! watchdog  division factor : 2
     */
    LL_WDT_PSR_2DIV,
    /*! watchdog  division factor : 4
     */
    LL_WDT_PSR_4DIV,
    /*! watchdog  division factor : 8
     */
    LL_WDT_PSR_8DIV,
    /*! watchdog  division factor : 16
     */
    LL_WDT_PSR_16DIV,
    /*! watchdog  division factor : 32
     */
    LL_WDT_PSR_32DIV,
    /*! watchdog  division factor : 64
     */
    LL_WDT_PSR_64DIV,
    /*! watchdog  division factor : 128
     */
    LL_WDT_PSR_128DIV,
    /*! watchdog  division factor : 256
     */
    LL_WDT_PSR_256DIV,
    /*! watchdog  division factor : 512
     */	
    LL_WDT_PSR_512DIV,
    /*! watchdog  division factor : 1024
     */	
    LL_WDT_PSR_1024DIV,
    /*! watchdog  division factor : 2048
     */	
    LL_WDT_PSR_2048DIV,
    /*! watchdog  division factor : 4096
     */
    LL_WDT_PSR_4096DIV,
    /*! watchdog  division factor : 8192
     */
    LL_WDT_PSR_8192DIV,
    /*! watchdog  division factor : 16384
     */
    LL_WDT_PSR_16384DIV,
    /*! watchdog  division factor : 32768
     */
    LL_WDT_PSR_32768DIV,
} TYPE_ENUM_LL_WDT_PSR;

/**
  * @brief Enumeration constant for low layer WDT mode selector
  */
typedef enum {
    /*! timeout watchdog response : Reset
     */
    LL_WDT_RMODE_RST = 0,
    /*! timeout watchdog response : interrupt
     */
    LL_WDT_RMODE_INT,
} TYPE_ENUM_LL_WDT_RMODE;


  
/***** LL API AND DRIVER API *****/



/**
  * @}
  */

/** @defgroup WDT_LL_Exported_Struct WDT LL Exported Struct
  * @ingroup  WDT_LL_Driver
  * @brief    WDT LL external configuration structure definition
  *
@verbatim   
  ===============================================================================
                                Exported Struct
  ===============================================================================  

    Exported Struct mainly extracts the WDT registers from the API, and abstracts 
    the structure. As long as it implements the low coupling between the registers 
    and the registers, the user only needs to configure the structure of the abstraction 
    layer and call hal_wdt_init. Function, you can configure the WDT module without 
    involving the configuration of the collective register.

@endverbatim
  *
  * @{
  */

/**
  * @brief WDT low layer init struct
  */
typedef struct __ll_wdt_init {
    /*! watchdog division factor
     */
    TYPE_ENUM_LL_WDT_PSR    psr;
    /*! watchdog mode,  timeout reset or int
     */
    TYPE_ENUM_LL_WDT_RMODE  mode;
} TYPE_LL_WDT_INIT;

/**
  * @}
  */

/** @defgroup WDT_LL_Interrupt WDT LL Interrupt Handle function
  * @brief   WDT LL Interrupt Handle function
  *
@verbatim   
  ===============================================================================
                        Interrupt Handle function
  ===============================================================================  

    This subsection provides a set of functions allowing to manage the WDT  
    Interrupt Handle function.

    how to use?

    The WDT interrupt handler uses a callback method that reserves the interface 
    to the user in the form of a callback function. The client needs to initialize 
    the callback function when initializing the WDT in order for the interrupt to 
    be processed normally. 
   
@endverbatim
  *
  * @{
  */



/**
  * @}
  */
  
/** @defgroup WDT_LL_Inti_Cfg WDT LL Initialization And Configuration
  * @brief    WDT LL Initialization And Configuration
  *
@verbatim   
  ===============================================================================
                        Initialization And Configuration
  ===============================================================================  

    This subsection provides a set of functions allowing to manage the WDT data 
    Initialization and Configuration.
    
    how to use?

@endverbatim
  *
  * @{
  */

/**
  * @brief  wdt_init
  * @param  p_wdt : pointer to the hardware WDT_TypeDef
  * @param  p_init: pointer to the init stuct TYPE_LL_WDT_INIT
  * @retval None
  */
void ll_wdt_init(WDT_TypeDef *p_wdt, TYPE_LL_WDT_INIT *p_init);

/**
  * @brief  wdt_deinit
  * @param  p_wdt: pointer to the hardware WDT_TypeDef
  * @retval None
  */
void ll_wdt_deinit(WDT_TypeDef *p_wdt);

/**
  * @brief  wdt_start
  * @param  p_wdt : pointer to the hardware WDT_TypeDef
  * @retval None
  */
void ll_wdt_start(WDT_TypeDef *p_wdt);

/**
  * @brief  wdt stop
  * @param  p_wdt: pointer to the hardware WDT_TypeDef
  * @retval None
  */
void ll_wdt_stop(WDT_TypeDef *p_wdt);


/**
  * @brief  ll_wdt_feed : use for clear watchdog counter
  * @param  p_wdt: pointer to the hardware WDT_TypeDef *p_wdt
  * @retval None
  */
void ll_wdt_feed(WDT_TypeDef *p_wdt);


/**
  * @}
  */
  
/** @defgroup WDT_LL_Data_Transfers WDT LL Data transfers functions
  * @brief    WDT LL Data transfers functions 
  *
@verbatim   
  ===============================================================================
                            Data transfers functions
  ===============================================================================  

    This subsection provides a set of functions allowing to manage the WDT data 
    transfers and receive.
  
@endverbatim
  *
  * @{
  */

/**
  * @brief  Used to indicate that get wdt int en
  * @param  p_wdt : pointer to the hardware WDT_TypeDef
  * @retval None
  */
#define LL_WDT_CON_INTERRUPT_GET(p_wdt)             ((p_wdt)->WDTCON & LL_WDT_CON_IE)

/**
  * @brief  Used to indicate that get wdt int pending
  * @param  p_wdt : pointer to the hardware WDT_TypeDef
  * @retval None
  */
#define LL_WDT_CON_PENDING_GET(p_wdt)               ((p_wdt)->WDTCON & LL_WDT_CON_PENDING)

/**
  * @brief  Used to indicate that get wdt int pending
  * @param  p_wdt : pointer to the hardware WDT_TypeDef
  * @retval None
  */
#define LL_WDT_CON_PENDING_CLR(p_wdt)               (LL_WDT_KEY_SET(p_wdt, LL_WDT_KEY_FEED_DOG))

/**
  * @brief  Used to indicate that set psr(division factor)
  * @param  p_wdt : pointer to the hardware WDT_TypeDef
  * @retval None
  */
#define LL_WDT_CON_PSR_SET(p_wdt, n)                do{LL_WDT_KEY_SET(p_wdt, LL_WDT_KEY_CAN_VISIT_PSR);(MODIFY_REG((p_wdt)->WDTCON,LL_WDT_CON_PSR(0xF),LL_WDT_CON_PSR(n)));} while(0)

/**
  * @brief  Used to indicate that get psr(division factor)
  * @param  p_wdt : pointer to the hardware WDT_TypeDef
  * @retval None
  */
#define LL_WDT_CON_PSR_GET(p_wdt)                   ((((p_wdt)->WDTCON)>>0) & LL_WDT_PSR(0xF))



/**
  * @}
  */

/**
  * @}
  */

#ifdef __cplusplus
}
#endif

/**
  * @}
  */

/**
  * @}
  */

#endif //__TS32FX_LL_WDT_H

/*************************** (C) COPYRIGHT 2018 TOPSYS ***** END OF FILE *****/
